package com.itheima.common;

import com.alibaba.fastjson.JSON;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Component
public class LoginInterceptor implements HandlerInterceptor {
    //判断是否登录
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //拦截浏览器
        if (request.getSession().getAttribute("employee")==null){
            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
            return false;
        }
//        //拦截手机端
//        if (request.getSession().getAttribute("user")==null){
//            response.getWriter().write(JSON.toJSONString(R.error("NOTLOGIN")));
//            return false;
//        }

        //放行原始方法
        return true;
    }

}
